package com.hqyj.base.controller;

import frame.base.controller.BaseController;
import com.hqyj.base.model.InfoUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @ProjectName: ERP-System
 * @Package: com.hqyj.base.controller
 * @ClassName: UserController
 * @Author: LiYi
 * @Date: 2021-09-06 14:40
 */

@Controller
@RequestMapping("/baseController")
public class InfoUserController extends BaseController {

    /*
    * 业务逻辑：
    *   （1）查询数据库InfoUser表中所有的数据记录
    * */
    @RequestMapping("/user.do")
    public String queryAllByInfoUser(Model model){
        // 查询所有InfoUser的信息
        List<InfoUser> infoUsers_db = infoUserService.queryAllByInfoUser();
        // 将查询的infoUsers_db数据传给前端
        model.addAttribute("infoUsers_db",infoUsers_db);
        return "view/base/infouser/infoUserList";
    }

    /*
    *添加InfoUser用户业务逻辑：
    *   （1）增加一条数据到InfoUser表
    *   （2）添加成功后，再次查询数据库所有InfoUser记录，确认是否添加成功
    *  */
    @RequestMapping("/addInfoUser.do")
    public String addInfoUser(Model model, InfoUser infoUser){
        infoUserService.addInfoUser(infoUser);
        List<InfoUser> infoUsers_db = infoUserService.queryAllByInfoUser();
        model.addAttribute("infoUsers_db",infoUsers_db);
        return "view/base/infouser/infoUserList";
    }

    /*
    * 根据Info User的Id删除业务逻辑：
    *   （1）根据页面发送请求中所携带需要删除当前记录的ID，删除此条记录
    *   （2）删除成功后，再次查询数据库所有InfoUser记录，确认是否删除成功
    * */
    @RequestMapping("/deleteUserById.do")
    public String deleteInfoUserById(Model model, String Id){
        int urlId = Integer.parseInt(Id);
        infoUserService.deleteInfoUserById(urlId);
        List<InfoUser> infoUsers_db = infoUserService.queryAllByInfoUser();
        model.addAttribute("infoUsers_db",infoUsers_db);
        return "view/base/infouser/infoUserList";
    }

    /*
    * 修改InfoUser中一条数据业务逻辑：
    *   （1）根据页面发送请求中所携带需要修改当前记录的ID，查询InfoUser表中当前Id的数据信息，返回到修改页面
    *   （2）页面提交请求后，再将修改的数据持久化
    *   （3）再次查询InfoUser所有数据记录
    * */
    @RequestMapping("/updateUI.do")
    public String queryInfoUserById(Model model, String Id){
        int urlId = Integer.parseInt(Id);
        InfoUser infoUser_db = infoUserService.queryInfoUserById(urlId);
        System.out.println("查看数据InfoUser_Db" + infoUser_db);
        model.addAttribute("infoUser_db",infoUser_db);
        return "view/base/infouser/updateInfoUser";
    }
    @RequestMapping("/updateInfoUserById.do")
    public String updateInfoUserById(Model model, InfoUser infoUser){
        infoUserService.updateInfoUserById(infoUser);
        List<InfoUser> infoUsers_db = infoUserService.queryAllByInfoUser();
        model.addAttribute("infoUsers_db",infoUsers_db);
        return "view/base/infouser/infoUserList";
    }

    /*
    * 模糊查询业务逻辑：
    *   （1）对InfoUser表（所有字段）进行模糊查询
    * */
    @RequestMapping("/queryFuzzy.do")
    public String queryFuzzy(Model model, String keyword){
        System.out.println("前端的Keyword" + keyword);
        List<InfoUser> infoUsers_db = infoUserService.queryFuzzy(keyword);
        for (InfoUser infoUser : infoUsers_db) {
            System.out.println(infoUser);
        }
        model.addAttribute("infoUsers_db",infoUsers_db);
        return "view/base/infouser/infoUserList";
    }
}
